package com.xzy.springcloud.config;

import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

/**
 * 配置路由bean方式
 * id-》唯一
 * r.path(路由的路径)
 *  r.path().(这个路由后到的地址)
 */
/*  国内的访问到国内的百度
    国外的访问到哔哩哔哩
 */
@Configuration
public class configGateWay {
    @Bean
    public RouteLocator custom(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_rote", new Function<PredicateSpec, Route.AsyncBuilder>() {
            public Route.AsyncBuilder apply(PredicateSpec r) {
                return r.path("/guonei").uri("http://news.baidu.com/guonei");
            }
        }).build();
        routes.route("path_rote", new Function<PredicateSpec, Route.AsyncBuilder>() {
            public Route.AsyncBuilder apply(PredicateSpec r) {
                return r.path("/guowai").uri("http://www.blibli.com");
            }
        }).build();
        return routes.build();
    }


}
